package com.sanben.demand.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanben.common.util.BeanCopyUtils;
import com.sanben.common.util.ServletUtils;
import com.sanben.demand.data.dto.DemandDto;
import com.sanben.demand.data.req.DemandReq;
import com.sanben.demand.data.vo.DemandVo;
import com.sanben.demand.entity.Demand;
import com.sanben.demand.mapper.DemandMapper;
import com.sanben.demand.service.IDemandService;
import com.sanben.region.service.IRegionService;
import com.sanben.tag.service.ITagRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author cyh
 * @description: 需求咨询表 服务实现类
 * @date: 2020-08-11
 */
@Service
public class DemandServiceImpl extends ServiceImpl<DemandMapper, Demand> implements IDemandService {

    @Autowired
    private IRegionService regionService;

    @Autowired
    private ITagRelationshipService tagRelationshipService;

    @Override
    public List<DemandVo> getHomeList() {
        List<DemandVo> list = baseMapper.getHomeList();
        list.stream().forEach(each -> {
            each.setRegionName(regionService.getById(each.getRegionId()).getName());
        });
        return list;
    }

    @Override
    public IPage<DemandVo> page(DemandReq req) {
        Page<DemandVo> page = new Page<>(req.getPageNum(), req.getPageSize());
        List<DemandVo> list = this.baseMapper.page(page, req);
        list.stream().forEach(each -> {
            //标签
            List<String> tagList = tagRelationshipService.getTagList(each.getId());
            each.setTagList(tagList);
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public Boolean save(DemandDto dto) {
        Demand demand = BeanCopyUtils.copy(dto, Demand.class);
        demand.setCreate_time(LocalDateTime.now());
        demand.setCreate_id(ServletUtils.getUserId());
        return this.save(demand);
    }

    @Override
    public Boolean updateById(DemandDto dto) {
        return this.updateById(BeanCopyUtils.copy(dto, Demand.class));
    }


    @Override
    public Boolean deleteLogic(List<Integer> toIntList) {
        return this.removeByIds(toIntList);
    }

    @Override
    public DemandVo detail(Integer id) {
        DemandVo demandVo = BeanCopyUtils.copy(this.getById(id), DemandVo.class);
        demandVo.setTagList(tagRelationshipService.getTagList(id));
        return demandVo;
    }
}
